package com.ma.netana.controller;

import com.ma.netana.dao.admin.AdminMapper;
import com.ma.netana.pojo.Admin;
import com.ma.netana.service.admin.AdminLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")

public class AdminController {

    @Autowired
    AdminMapper adminMapper;


    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(HttpSession httpSession, @RequestParam("admin_name") String admin_name, @RequestParam("admin_password")String admin_password, Model model){
        System.out.println("========login=======");
        System.out.println("name:"+admin_name + " " + "password:" + " " + admin_password);
        String info = "";
        Admin admin = adminMapper.getLoginUser(admin_name);
        System.out.println(admin);
        if (admin.getAdminName() != null){
            info = "OK";
        }else {
            info = "无此用户！！";
        }
        if (admin.getAdminPassword().equals(admin_password)){
            httpSession.setAttribute("admin",admin);
            model.addAttribute("msg","登陆成功");
            info = "密码输入正确";
            return "redirect:/main.html";
        }else {
            model.addAttribute("msg","error");
            return "index";
        }
    }

    @RequestMapping("/logout")
    public String loginOut(HttpSession session,Model model){
        System.out.println("=====loginOut=====");
        session.invalidate();
        return "redirect:/index.html";
    }

}
